package observer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.webrtc.SdpObserver;
import org.webrtc.SessionDescription;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class SessionObserver implements SdpObserver {
    private final ExecutorService executor;
    private static final Logger logger = LoggerFactory.getLogger(SessionObserver.class);
    private SessionDescription sdp;

    public SessionObserver() {
        executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    }
    @Override
    public void onCreateSuccess(SessionDescription sessionDescription) {
        this.sdp = sessionDescription;
        executor.shutdown();
    }


    public SessionDescription getSdp() {
        try {
            executor.awaitTermination(3000, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (sdp == null) {
            throw new IllegalStateException("Cannot get the SDP.");
        }
        return sdp;
    }

    @Override
    public void onSetSuccess() {
        logger.info("Successfully create local SDP.");
    }

    @Override
    public void onCreateFailure(String s) {

    }

    @Override
    public void onSetFailure(String s) {

    }

}

